<?php
/**
 * Created by PhpStorm.
 * User: Bixue
 * Date: 18-9-14
 */
namespace app\workerman\model;

use \GatewayWorker\Lib\Gateway as Client;
use think\Db;

class Trade
{
    const TAB_NAME_QUOTATION   = "quotation";
    const TAB_NAME_QUOTATIONBS = "quotation_bs";
    const TAB_NAME_TRADE       = "trade";

    /**
     * sendData
     * @author     wang
     * @deprecated 对产品数据实时推送 （最新价格，买卖量等。。）
     * @return void
     */
    public function sendData()
    {
        Client::$registerAddress = '127.0.0.1:8201';

        $tData = db(self::TAB_NAME_TRADE,[],false)
                   ->alias('t')
                   ->join(self::TAB_NAME_QUOTATIONBS.' bs', 't.id=bs.trade_id', 'left')
                   ->field([
                       't.id',
                       't.last_trading_price',
                       't.high_price',
                       't.low_price',
                       't.open_price',
                       't.close_price',
                       't.diff',
                       't.diff_rate',
                       't.sell_count',
                       't.buy_count',
                       'bs.s1_price',
                       'bs.s1_count',
                       'bs.s2_price',
                       'bs.s2_count',
                       'bs.s3_price',
                       'bs.s3_count',
                       'bs.s4_price',
                       'bs.s4_count',
                       'bs.s5_price',
                       'bs.s5_count',
                       'bs.b1_price',
                       'bs.b1_count',
                       'bs.b2_price',
                       'bs.b2_count',
                       'bs.b3_price',
                       'bs.b3_count',
                       'bs.b4_price',
                       'bs.b4_count',
                       'bs.b5_price',
                       'bs.b5_count',
                   ])
                   ->where('t.available',1)
                   ->select();
        if($tData) {
            foreach ($tData as $val) {
                $message = json_encode(array('list' => $val,'type' => 'data'));
                Client::sendToGroup('data_'.$val['id'],$message);
            }
        }
    }

    /**
     * getYstTrade
     * @author getYstTrade
     * @date   2018/9/19 17:23
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getYstTrade()
    {
        $res = Db::name(self::TAB_NAME_TRADE, [], false)
                 ->whereTime('update_time', 'yesterday')
                 ->where('delete_time is null')
                 ->where('available', '1')
                 ->order('update_time', 'desc')
                 ->select();

        return $res;
    }

    /**
     * getYstNewPrice
     * @author getYstNewPrice
     * @date   2018/9/19 17:23
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getYstNewPrice(string $tradeId)
    {
        $res = Db::name(self::TAB_NAME_QUOTATION, [], false)
                 ->field([
                     'price_now',
                     'update_time',
                 ])
                 ->where('trade_id', $tradeId)
                 ->whereTime('update_time', 'yesterday')
                 ->order('update_time', 'desc')
                 ->select();

        return $res;
    }

    /**
     * updateTrade
     * @author Bixue
     * @date   2018/9/19 17:46
     * @param int   $tradeId
     * @param array $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function updateTrade(string $tradeId, array $data)
    {
        $map = [
            'sell_count'    => '0',
            'buy_count'     => '0',
            'high_price'    => '',
            'low_price'     => '',
            'open_price'    => '',
            'close_price'   => $data['closePrice'],
            'trading_count' => '',
            'update_time'   => date('Y:m:d H:i:s'),
        ];
        $res = Db::name(self::TAB_NAME_TRADE, [], false)
                 ->where('id', $tradeId)
                 ->update($map);
        return $res;
    }

}